package com.oocl.sc.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.oocl.sc.dao.ScartDao;
import com.oocl.sc.util.DbUtil;

public class ScartDaoImpl implements ScartDao {

	public int updateMenu(String account, String menulist) {
		String sql = "update scart set menulist=? where account=?";
		Connection conn = null;
		PreparedStatement st = null;
		int m = 0;

		try {
			conn = DbUtil.getConnection();
			st = conn.prepareStatement(sql);
			st.setString(1, menulist);
			st.setString(2, account);
			m = st.executeUpdate();

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DbUtil.free(conn, st, null);
		}
		return m;
	}

	public int deleteMenu(String account) {
		String sql = "delete from scart where account=?";
		Connection conn = null;
		PreparedStatement st = null;
		int m = 0;
		try {
			conn = DbUtil.getConnection();
			st = conn.prepareStatement(sql);
			st.setString(1, account);
			m = st.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DbUtil.free(conn, st, null);
		}
		return m;
	}

	public int addMenu(String account, String menulist) {
		String sql = "insert into scart(cid,account,menulist) values(scart_seq.Nextval,?,?)";
		Connection conn = null;
		PreparedStatement st = null;
		int m = 0;
		try {
			conn = DbUtil.getConnection();
			st = conn.prepareStatement(sql);
			st.setString(1, account);
			st.setString(2, menulist);
			m = st.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DbUtil.free(conn, st, null);
		}
		return m;
	}

	public String loadScartByAccount(String account) {
		String sql = "select * from scart where account=?";
		Connection conn = null;
		PreparedStatement st = null;
		ResultSet rs = null;
		String result = "";
		try {
			conn = DbUtil.getConnection();
			st = conn.prepareStatement(sql);
			st.setString(1, account);
			rs = st.executeQuery();
			if (rs.next()) {
				result = rs.getString("menulist");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DbUtil.free(conn, st, rs);
		}
		return result;
	}

	public int isAccountExisted(String account) {
		String sql = "select * from scart where account=?";
		Connection conn = null;
		PreparedStatement st = null;
		ResultSet rs = null;
		try {
			conn = DbUtil.getConnection();
			st = conn.prepareStatement(sql);
			st.setString(1, account);
			rs = st.executeQuery();
			if (rs.next()) {
				return 1;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DbUtil.free(conn, st, rs);
		}
		return 0;
	}

}
